package net.homelinux.chaoswg.io.renga.krypto.animations.Merge4;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;
import net.homelinux.chaoswg.io.renga.krypto.animations.Merge4.Merge4State;
import net.homelinux.chaoswg.io.renga.krypto.animations.Merge4.Merge4StateChanger;

public class Merge4Animation extends Animation {
    Merge4State initialState;
    Merge4StateChanger changer;
    
    public Merge4Animation () {
        initialize(new Merge4State());
    }

    public Merge4Animation (Merge4State initialState) {
        initialize(initialState);
    }

    private void initialize(State initialState) {
        if (!(initialState instanceof Merge4State)) {
            throw new IllegalArgumentException ();
        } 
        this.initialState = ((Merge4State)initialState);
        this.changer = new Merge4StateChanger (this.initialState);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (changer);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        changer.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }
    
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }

}
